home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Graphics Programming (2nd Edition)
/
Visual Basic Graphics Programming 2nd Edition.iso
/
OldSrc
/
CH10
/
SRC
/
TRANS.FRM
< prev
next >
Wrap
Text File
|
1996-05-02
|
22KB
|
761 lines
VERSION 4.00
Begin VB.Form TransformedForm
Appearance = 0 'Flat
BackColor = &H00C0C0C0&
Caption = "Surfaces of Transformation"
ClientHeight = 5700
ClientLeft = 690
ClientTop = 900
ClientWidth = 7830
BeginProperty Font
name = "MS Sans Serif"
charset = 1
weight = 700
size = 8.25
underline = 0 'False
italic = 0 'False
strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 6390
KeyPreview = -1 'True
Left = 630
LinkTopic = "Form1"
ScaleHeight = 380
ScaleMode = 3 'Pixel
ScaleWidth = 522
Top = 270
Width = 7950
Begin VB.CommandButton CmdCreate
Caption = "Transform"
Height = 495
Left = 600
TabIndex = 19
Top = 5040
Width = 1095
End
Begin VB.Frame Frame1
Caption = "Transformations"
Height = 2055
Left = 0
TabIndex = 14
Top = 2880
Width = 2295
Begin VB.OptionButton TransChoice
Caption = "Wierd"
Height = 255
Index = 4
Left = 120
TabIndex = 22
Top = 1680
Width = 2055
End
Begin VB.OptionButton TransChoice
Caption = "Up, Shrink/Grow"
Height = 255
Index = 3
Left = 120
TabIndex = 18
Top = 1320
Width = 2055
End
Begin VB.OptionButton TransChoice
Caption = "Up, Shrink, Twist"
Height = 255
Index = 2
Left = 120
TabIndex = 17
Top = 960
Width = 2055
End
Begin VB.OptionButton TransChoice
Caption = "Up, Shrink"
Height = 255
Index = 1
Left = 120
TabIndex = 16
Top = 600
Width = 2055
End
Begin VB.OptionButton TransChoice
Caption = "Up, Twist"
Height = 255
Index = 0
Left = 120
TabIndex = 15
Top = 240
Value = -1 'True
Width = 2055
End
End
Begin VB.Frame Frame2
Caption = "Curve"
Height = 2775
Left = 0
TabIndex = 8
Top = 0
Width = 2295
Begin VB.OptionButton CurveChoice
Caption = "Semicircle"
Height = 255
Index = 6
Left = 120
TabIndex = 21
Top = 2400
Width = 2055
End
Begin VB.OptionButton CurveChoice
Caption = "Line Segment"
Height = 255
Index = 0
Left = 120
TabIndex = 20
Top = 240
Value = -1 'True
Width = 2055
End
Begin VB.OptionButton CurveChoice
Caption = "Star"
Height = 255
Index = 5
Left = 120
TabIndex = 13
Top = 2040
Width = 2055
End
Begin VB.OptionButton CurveChoice
Caption = "Off Center Circle"
Height = 255
Index = 4
Left = 120
TabIndex = 12
Top = 1680
Width = 2055
End
Begin VB.OptionButton CurveChoice
Caption = "Circle"
Height = 255
Index = 3
Left = 120
TabIndex = 11
Top = 1320
Width = 2055
End
Begin VB.OptionButton CurveChoice
Caption = "Off Center Square"
Height = 255
Index = 2
Left = 120
TabIndex = 10
Top = 960
Width = 2055
End
Begin VB.OptionButton CurveChoice
Caption = "Square"
Height = 255
Index = 1
Left = 120
TabIndex = 9
Top = 600
Width = 2055
End
End
Begin VB.CheckBox ShowAxesCheck
Caption = "Show Axes"
Height = 255
Left = 2400
TabIndex = 7
Top = 5400
Width = 1335
End
Begin VB.TextBox PhiText
Height = 285
Left = 6960
TabIndex = 6
Text = "0.1570"
Top = 5400
Width = 855
End
Begin VB.TextBox ThetaText
Height = 285
Left = 5640
TabIndex = 4
Text = "0.6283"
Top = 5400
Width = 855
End
Begin VB.TextBox RText
Height = 285
Left = 4080
TabIndex = 2
Text = "10"
Top = 5400
Width = 855
End
Begin VB.PictureBox Pict
AutoRedraw = -1 'True
Height = 5295
Left = 2400
ScaleHeight = 349
ScaleMode = 3 'Pixel
ScaleWidth = 357
TabIndex = 0
Top = 0
Width = 5415
End
Begin MSComDlg.CommonDialog LoadDialog
Left = 1800
Top = 5280
_version = 65536
_extentx = 847
_extenty = 847
_stockprops = 0
cancelerror = -1 'True
End
Begin VB.Label Label1
Caption = "Phi"
Height = 255
Index = 2
Left = 6600
TabIndex = 5
Top = 5415
Width = 375
End
Begin VB.Label Label1
Caption = "Theta"
Height = 255
Index = 1
Left = 5040
TabIndex = 3
Top = 5415
Width = 495
End
Begin VB.Label Label1
Caption = "R"
Height = 255
Index = 0
Left = 3840
TabIndex = 1
Top = 5415
Width = 255
End
Begin VB.Menu mnuFile
Caption = "&File"
Begin VB.Menu mnuFileLoad
Caption = "&Load..."
Shortcut = ^L
End
Begin VB.Menu mnuFileSaveAs
Caption = "&Save As..."
Shortcut = ^A
End
Begin VB.Menu mnuFileSep
Caption = "-"
End
Begin VB.Menu mnuFileExit
Caption = "E&xit"
End
End
End
Attribute VB_Name = "TransformedForm"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Option Explicit
' Location of viewing eye.
Dim EyeR As Single
Dim EyeTheta As Single
Dim EyePhi As Single
Const dtheta = PI / 20
Const Dphi = PI / 20
Const dR = 1
' Location of focus point.
Const FocusX = 0#
Const FocusY = 0#
Const FocusZ = 0#
Dim Projector(1 To 4, 1 To 4) As Single
Dim CurveNum As Integer
Dim TransNum As Integer
Dim NumTrans As Integer
Dim Trans() As Transformation
Dim ThePicture As ObjPicture
Dim TheSurface As ObjTransformed
Dim ShowingParameters As Boolean
' ************************************************
' Create the selected curve.
' ************************************************
Sub CreateCurve()
Dim R As Single
Dim R2 As Single
Dim dtheta As Single
Dim theta As Single
Dim y As Single
Dim i As Integer
Select Case CurveNum
Case 0 ' Line segment.
TheSurface.AddCurvePoint -2, 0, 0
TheSurface.AddCurvePoint 2, 0, 0
Case 1 ' Square.
TheSurface.AddCurvePoint -2, 0, -2
TheSurface.AddCurvePoint -2, 0, 2
TheSurface.AddCurvePoint 2, 0, 2
TheSurface.AddCurvePoint 2, 0, -2
TheSurface.AddCurvePoint -2, 0, -2
Case 2 ' Off Center Square.
TheSurface.AddCurvePoint 1, 0, 1
TheSurface.AddCurvePoint 1, 0, 3
TheSurface.AddCurvePoint 3, 0, 3
TheSurface.AddCurvePoint 3, 0, 1
TheSurface.AddCurvePoint 1, 0, 1
Case 3 ' Circle.
R = 2
dtheta = PI / 8
For theta = 0 To 2 * PI - dtheta + 0.01 Step dtheta
TheSurface.AddCurvePoint R * Cos(theta), 0, R * Sin(theta)
Next theta
TheSurface.AddCurvePoint R, 0, 0
Case 4 ' Off Center Circle.
R = 1
dtheta = PI / 8
For theta = 0 To 2 * PI - dtheta + 0.01 Step dtheta
TheSurface.AddCurvePoint 2 + R * Cos(theta), 0, 2 + R * Sin(theta)
Next theta
TheSurface.AddCurvePoint 2 + R, 0, 2
Case 5 ' Star.
R = 2
R2 = 1
dtheta = 2 * PI / 5 / 2
theta = PI
For i = 1 To 5
TheSurface.AddCurvePoint _
R * Cos(theta), 0, R * Sin(theta)
theta = theta + dtheta
TheSurface.AddCurvePoint _
R2 * Cos(theta), 0, R2 * Sin(theta)
theta = theta + dtheta
Next i
TheSurface.AddCurvePoint _
R * Cos(PI), 0, R * Sin(PI)
Case 6 ' Semicircle.
R = 2
dtheta = PI / 8
For theta = 0 To PI - dtheta + 0.01 Step dtheta
TheSurface.AddCurvePoint R * Cos(theta), 0, R * Sin(theta)
Next theta
TheSurface.AddCurvePoint -R, 0, 0
Case Else
Beep
End Select
End Sub
' ************************************************
' Create the array of transformations.
' ************************************************
Sub CreateTransformations()
Dim A(1 To 4, 1 To 4) As Single
Dim B(1 To 4, 1 To 4) As Single
Dim C(1 To 4, 1 To 4) As Single
Dim theta As Single
Dim dtheta As Single
Dim R As Single
Dim y As Single
Dim i As Integer
Select Case TransNum
Case 0 ' Up, twist.
NumTrans = 9
ReDim Trans(1 To NumTrans)
dtheta = PI / 12
For i = 1 To NumTrans
y = i / 2
theta = i * dtheta
m3Translate A, 0, y, 0 ' Translate.
m3YRotate B, theta ' Rotate.
m3MatMultiply Trans(i).M, A, B ' Combine.
Next i
Case 1 ' Up, shrink.
NumTrans = 9
ReDim Trans(1 To NumTrans)
For i = 1 To NumTrans
y = i / 2
R = (NumTrans - i) / NumTrans
m3Scale A, R, 1, R ' Scale.
m3Translate B, 0, y, 0 ' Translate.
m3MatMultiply Trans(i).M, A, B ' Combine.
Next i
Case 2 ' Up, shrink, twist.
NumTrans = 9
ReDim Trans(1 To NumTrans)
dtheta = PI / 12
For i = 1 To NumTrans
y = i / 2
R = (NumTrans - i) / NumTrans
theta = i * dtheta
m3Scale A, R, 1, R ' Scale.
m3Translate B, 0, y, 0 ' Translate.
m3MatMultiply C, A, B ' Combine A and B.
m3YRotate A, theta ' Rotate.
m3MatMultiply Trans(i).M, C, A ' Combine all.
Next i
Case 3 ' Up, shrink/grow.
NumTrans = 18
ReDim Trans(1 To NumTrans)
dtheta = PI / 12
For i = 1 To NumTrans
y = i / 4
theta = i * dtheta
R = 1 + Sin(2 * theta) / 2
m3Scale A, R, 1, R ' Scale.
m3Translate B, 0, y, 0 ' Translate.
m3MatMultiply Trans(i).M, A, B ' Combine.
Next i
Case 4 ' Waver.
' Make the curve move upwards with
' varying rotation around the Z axis.
NumTrans = 18
ReDim Trans(1 To NumTrans)
dtheta = PI / 12
R = PI / 2
For i = 1 To NumTrans
y = i / 4
theta = i * dtheta
m3ZRotate A, R * Sin(theta) ' Rotate.
m3Translate B, 0, y, 0 ' Translate.
m3MatMultiply Trans(i).M, A, B ' Combine.
Next i
Case Else
Beep
End Select
End Sub
Sub WaitEnd()
MousePointer = vbDefault
End Sub
Sub WaitStart()
MousePointer = vbHourglass
DoEvents
End Sub
' ************************************************
' Create the surface.
' ************************************************
Private Sub CmdCreate_Click()
Dim pline As ObjPolyline
Dim i As Integer
WaitStart
Set ThePicture = New ObjPicture
Set TheSurface = New ObjTransformed
ThePicture.objects.Add TheSurface
CreateCurve
CreateTransformations
For i = 1 To NumTrans
TheSurface.SetTrans Trans(i).M
Next i
TheSurface.Transform
If ShowAxesCheck.value = vbChecked Then
Set pline = New ObjPolyline
ThePicture.objects.Add pline
pline.AddSegment 0, 0, 0, 5, 0, 0
pline.AddSegment 0, 0, 0, 0, 5, 0
pline.AddSegment 0, 0, 0, 0, 0, 5
End If
DrawData Pict
Pict.SetFocus
End Sub
' ************************************************
' Save the current curve choice.
' ************************************************
Private Sub CurveChoice_Click(Index As Integer)
CurveNum = Index
End Sub
' *******************************************************
' Rotate the points in the cube and draw the cube.
' *******************************************************
Private Sub DrawData(pic As Object)
Dim x As Single
Dim y As Single
Dim z As Single
Dim S(1 To 4, 1 To 4) As Single
Dim t(1 To 4, 1 To 4) As Single
Dim ST(1 To 4, 1 To 4) As Single
Dim PST(1 To 4, 1 To 4) As Single
MousePointer = vbHourglass
Refresh
' Prevent overflow errors when drawing lines
' too far out of bounds.
On Error Resume Next
' Scale and translate so it looks OK in pixels.
m3Scale S, 35, -35, 1
m3Translate t, 180, 250, 0
m3MatMultiplyFull ST, S, t
m3MatMultiplyFull PST, Projector, ST
' Transform the points.
ThePicture.ApplyFull PST
' Display the data.
pic.Cls
ThePicture.Draw pic, EyeR
pic.Refresh
' Display the viewnig parameters.
ShowViewingParameters
MousePointer = vbDefault
End Sub
Sub ShowViewingParameters()
ShowingParameters = True
RText.Text = Format$(EyeR, "0.0000")
ThetaText.Text = Format$(EyeTheta, "0.0000")
PhiText.Text = Format$(EyePhi, "0.0000")
RText.Refresh
ThetaText.Refresh
PhiText.Refresh
ShowingParameters = False
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft
EyeTheta = EyeTheta - dtheta
Case vbKeyRight
EyeTheta = EyeTheta + dtheta
Case vbKeyUp
EyePhi = EyePhi - Dphi
Case vbKeyDown
EyePhi = EyePhi + Dphi
Case Else
Exit Sub
End Select
m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
DrawData Pict
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("+")
EyeR = EyeR + dR
Case Asc("-")
EyeR = EyeR - dR
Case Else
Exit Sub
End Select
m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
DrawData Pict
End Sub
Private Sub Form_Load()
' Initialize the eye position.
EyeR = 10
EyeTheta = PI * 0.2
EyePhi = PI * 0.1
' Initialize the projection transformation.
m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
Me.Show
CurveChoice_Click 0
End Sub
Private Sub mnuFileExit_Click()
Unload Me
End Sub
Private Sub mnuFileLoad_Click()
Dim fname As String
Dim filenum As Integer
Dim txt As String
Dim Xmin As Single
Dim ymin As Single
Dim xmax As Single
Dim ymax As Single
Dim i As Integer
' Allow the user to pick a file.
On Error Resume Next
LoadDialog.filename = "*.APF"
LoadDialog.ShowOpen
If Err.Number = cdlCancel Then
Unload LoadDialog
Exit Sub
ElseIf Err.Number <> 0 Then
Unload LoadDialog
Beep
MsgBox "Error selecting file.", , vbExclamation
Exit Sub
End If
On Error GoTo 0
fname = LoadDialog.filename
LoadDialog.InitDir = Left$(fname, Len(fname) _
- Len(LoadDialog.FileTitle) - 1)
' Clear the picture.
Set ThePicture = Nothing
' Open the file.
filenum = FreeFile
Open fname For Input As #filenum
' Make sure it's an Object Picture File.
Input #filenum, txt
If txt <> "3D APF PICTURE" Then
Close filenum
Beep
MsgBox "Error reading file """ & fname & """.", , vbExclamation
Exit Sub
End If
' Read the picture.
MousePointer = vbHourglass
DoEvents
Set ThePicture = New ObjPicture
ThePicture.FileInput filenum
' Close the file.
Close filenum
' Refresh the display.
DrawData Pict
' Deselect all the option buttons.
For i = 0 To 6
If CurveChoice(i).value Then _
CurveChoice(i).value = False
Next i
For i = 0 To 4
If TransChoice(i).value Then _
TransChoice(i).value = False
Next i
MousePointer = vbDefault
End Sub
Private Sub mnuFileSaveAs_Click()
Dim fname As String
Dim filenum As Integer
' Allow the user to pick a file.
On Error Resume Next
LoadDialog.filename = "*.APF"
LoadDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
LoadDialog.ShowSave
If Err.Number = cdlCancel Then
Unload LoadDialog
Exit Sub
ElseIf Err.Number <> 0 Then
Unload LoadDialog
Beep
MsgBox "Error selecting file.", , vbExclamation
Exit Sub
End If
On Error GoTo 0
fname = LoadDialog.filename
LoadDialog.InitDir = Left$(fname, Len(fname) _
- Len(LoadDialog.FileTitle) - 1)
' Open the file.
filenum = FreeFile
Open fname For Output As #filenum
' Write the picture.
ThePicture.FileWrite filenum
' Close the file.
Close filenum
End Sub
Private Sub PhiText_Change()
If ShowingParameters Then Exit Sub
EyePhi = CSng(PhiText.Text)
m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
DrawData Pict
End Sub
Private Sub RText_Change()
If ShowingParameters Then Exit Sub
EyeR = CSng(RText.Text)
m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
DrawData Pict
End Sub
' ************************************************
' Redraw with the axes on or off as appropriate.
' ************************************************
Private Sub ShowAxesCheck_Click()
CmdCreate_Click
End Sub
Private Sub ThetaText_Change()
If ShowingParameters Then Exit Sub
EyeTheta = CSng(ThetaText.Text)
m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
DrawData Pict
End Sub
' ************************************************
' Save the current transformation choice.
' ************************************************
Private Sub TransChoice_Click(Index As Integer)
TransNum = Index
End Sub